//Copyright (C) 1998-2008, Sumisho Computer Systems Corp. All Rights Reserved.

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at

//http://www.apache.org/licenses/LICENSE-2.0

//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

/**
 * @author Hitoshi Okada
 */

package com.curlap.orb.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.curlap.orb.security.AccessControlFilter;
import com.curlap.orb.context.AbstractApplicationContext;
import com.curlap.orb.context.ApplicationContextFactory;

/**
 * InvokeInContextServlet
 */
public class InvokeInContextServlet extends InstanceManagementServlet
{
	private static final long serialVersionUID = 1L;

    private final Log log = LogFactory.getLog(InvokeInContextServlet.class);

    @Override
    public void init()
    {
        // Load ApplicationContext
        final AbstractApplicationContext context = 
            (ApplicationContextFactory.getInstance(getServletConfig().getServletContext())).getApplicationContext();
        log.debug("Loaded ApplicationContext [" + context.getClass().getName() + "]");
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException
    {
        super.doPost(request, response);
        final InvokeInContextRequest invokeRequest = 
            (InvokeInContextRequest)request.getAttribute(InstanceManagementServlet.REQUEST_OBJECT);
        try
        {
            final CurlConfig config = (CurlConfigFactory.getInstance(getServletContext())).getCurlConfig();
            if (!config.enabledApplicationContext())
                throw new InstanceManagementException("ApplicationContext is disabled.");

            final String className = invokeRequest.getClassName(); // context's object name
            final String methodName = invokeRequest.getMethodName();
            final Object[] arguments = invokeRequest.getArguments();
            
            // - Security - access control for interface(class and method)
            (AccessControlFilter.getInstance(config.getAccessControlList())).checkAccessControlList(className, methodName, arguments);
            
            final Object obj = ApplicationContextFactory.getInstance(getServletContext()).getApplicationContext().getObject(className);
            if(obj == null)
                throw new InstanceManagementException("This object does not exsit [" + className + "]");
            request.setAttribute(InstanceManagementServlet.RESPONSE_OBJECT, MethodUtils.invokeMethod(obj, methodName, arguments));
            // debug
            log.debug("Request invoke(DI Container) [" + className + "." + methodName + " " + arguments + "]");
        }
        // IOException, SerializerException, ApplicationContextException, InstanceManagementException ...
        catch(Exception e)
        {
            request.setAttribute(InstanceManagementServlet.RESPONSE_OBJECT, e);
        }
    }
}
